using Crux.Core.Websockets.Metadata;
using Crux.Core.Websockets.Redis.Metadata;
using Crux.Core.Websockets.Redis.Utils;

namespace Crux.Core.Websockets.Redis;
/// <summary>
/// MessageResolver
/// </summary>
public class MessageResolver
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="buffers"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    internal static InternalMessage DeserializeInternalMessage(byte[] buffers, CancellationToken cancellationToken = default)
        => Deserialize<InternalMessage>(buffers, cancellationToken);
    /// <summary>
    ///
    /// </summary>
    /// <param name="buffers"></param>
    /// <returns></returns>
    internal static GroupCommand DeserializeGroupCommand(byte[] buffers)
        => Deserialize<GroupCommand>(buffers);
    /// <summary>
    ///
    /// </summary>
    /// <param name="buffers"></param>
    /// <param name="cancellationToken"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    private static T Deserialize<T>(byte[] buffers, CancellationToken cancellationToken = default)
    {
        Requires.NotNull(buffers, nameof(buffers));

        return MessagePackUtils.Deserialize<T>(buffers, cancellationToken: cancellationToken);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    /// <param name="cancellationToken"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    private static byte[] Serialize<T>(T t, CancellationToken cancellationToken = default)
    {
        Requires.NotNull(t, nameof(T));

        return MessagePackUtils.Serialize(t, cancellationToken: cancellationToken);
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="message"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    internal static byte[] SerializeInternalMessage(InternalMessage message, CancellationToken cancellationToken = default)
        => Serialize(message, cancellationToken);
    /// <summary>
    ///
    /// </summary>
    /// <param name="command"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    internal static byte[] SerializeGroupCommand(GroupCommand command, CancellationToken cancellationToken = default)
        => Serialize(command, cancellationToken);
}